package com.ych.spring.aop.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author yangchenghao
 */
@Component
@Aspect
public class LoggerAspect {
    @Pointcut("execution(* com.ych.spring.aop.annotation.CalculatorImpl.*(..))")
    public void pointcut() {
    }

    //    @Before("execution(* com.ych.spring.aop.annotation.CalculatorImpl.*(..))")
//    @Before("pointcut()")
//    public void beforeMethod(JoinPoint joinPoint) {
//        Signature signature = joinPoint.getSignature();
//        System.out.println("日志：方法" + signature.getName() + ",参数" + Arrays.toString(joinPoint.getArgs()));
//    }
//
//    @After("pointcut()")
//    public void AfterMethod(JoinPoint joinPoint) {
//        Signature signature = joinPoint.getSignature();
//        System.out.println("日志：方法" + signature.getName() + "执行完毕");
//    }
//
//    @AfterReturning(value = "pointcut()", returning = "result")
//    public void AfterReturnMethod(JoinPoint joinPoint, Object result) {
//        Signature signature = joinPoint.getSignature();
//        System.out.println("日志：方法" + signature.getName() + "，结果：" + result);
//    }
//
//    @AfterThrowing(value = "pointcut()", throwing = "e")
//    public void AfterThrowMethod(JoinPoint joinPoint, Exception e) {
//        Signature signature = joinPoint.getSignature();
//        System.out.println("日志：方法" + signature.getName() + "，异常" + e);
//    }

    @Around("pointcut()")
    public Object AroundMethod(ProceedingJoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object result = null;
        try {
            System.out.println("日志：方法" + signature.getName() + "，参数：" + Arrays.toString(joinPoint.getArgs()));
            result = joinPoint.proceed();
            System.out.println("日志：方法" + signature.getName() + "，结果" + result);
        } catch (Throwable e) {
            System.out.println("日志：方法" + signature.getName() + "，异常：" + e);
        } finally {
            System.out.println("日志：方法" + signature.getName() + "执行完毕");
        }
        return result;

    }
}
